package com.pdh.filter;

import com.pdh.bean.Message;
import com.pdh.util.LoginUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 彭德华
 * @Date 2021-9-29 22:19
 */
@WebFilter({"/admin/index.html","/admin/views/*","/express/*"})//过滤器还未启用
public class AccessControlFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        Message adminInfo = LoginUtil.getAdminInfoMsg(request.getSession());
        if(adminInfo != null){
            filterChain.doFilter(servletRequest,servletResponse);//放行
        }else {
            //response.sendError(404,"很遗憾,您的权限不足,请登陆");
            response.sendRedirect("/admin/login.html");//选择跳转到admin/login.html页面
        }
    }

    @Override
    public void destroy() {

    }
}
